package admin_service

import (
	"fmt"
	"shop/config/constant"
	"shop/dao"
	"shop/entity"
	"shop/utils/result"
)

func GetUserList(shopId, pageNo, pageSize int) *result.Result {
	list := make([]*entity.User, 0, 0)
	total := new(int64)
	db := dao.GetDB().Table(entity.TableNameUser).Where("shop_id", shopId).Count(total)
	db.Offset((pageNo - 1) * pageSize).Limit(pageSize).Find(&list)
	return result.Ok(entity.Page{
		Total: *total,
		List:  list,
	})
}

func UpdateUserStatus(userId int64, shopId int, newStatus int) *result.Result {
	if newStatus != constant.UserStatusNormal && newStatus != constant.UserStatusFrozen {
		return result.Fail(fmt.Sprintf("非法状态值: %d", newStatus))
	}
	rows := dao.GetTx().Table(entity.TableNameUser).
		Where("id=? and shop_id=?", userId, shopId).
		UpdateColumn("status", newStatus).Commit().RowsAffected
	return result.Ok(rows)
}
